<?php
/*
 * @分销商城
 * @功能说明：幻灯管理
 * @更新说明：暂无更新
 * @文件名 SlideController.class.php
 * @编码 UTF-8
 * @创建时间 2015-10-27 下午6:49:41
 * @创建人 XING
 */

namespace dg\Controller;
use Think\Controller;

class SlideController extends ApibaseController {
	/*
	 * 分销商城_幻灯主页
	 * @date: 2015-10-27 下午6:49:56
	 * @editor: XING
	 */
	public function index(){
		$page = I('get.page') ?: 1;
		$perpage = 20;$url = '';
		$list = M('SlideEntity');
		$url = array();
		if(I('get.slidename')){
			$list = $list->where('title LIKE "%'.I('get.slidename').'%"');
			$url['slidename'] = I('get.slidename');
		}
		if(I('get.slideid')){
			$list = $list->where(array('slide_id'=>I('get.slideid')));
			$url['slideid'] = I('get.slideid');
		}
		$getObj = clone $list;
		$slideData = $list->order(array('sort'=>'desc'))->page($page,$perpage)->select();
		
		$this->assign('page',setPage($getObj->count(),$perpage,$page,'?'.http_build_query($url)));
		$this->assign('slideData',$slideData);
		$this->display('slide/list');		
	}
	
	/*
	 * 添加幻灯片
	 * @date: 2015-10-25下午2:00:19
	 * @editor: XING
	 */
	public function addslide(){
		if(I('post.dosubmit')){
			if(I('post.slide')){
				$updata = M('SlideEntity')->where(array(
							'slide_id' => I('post.slide')
						))->setField(array(
							'title' => I('post.title'),
							'sort' => I('post.sort'),
							'max_num' => I('post.max_num') ?: 3,
							'update_at' => time()
						));
				if($updata){
					putlog('幻灯片类别更新 ID`'.I('post.slide').'`','SlideController','addslide');
					$this->showtips('更新幻灯类别成功');
				}else{
					$this->showtips('操作失败');
				}
			}
			$addId = M('SlideEntity')->add(array(
					'title' => I('post.title'),
					'sort' => I('post.sort'),
					'max_num' => I('post.max_num') ?: 3,
					'add_at' => time(),
					'update_at' => time()
			));
			if($addId){
				putlog('幻灯片类别添加 ID`'.$addId.'`','SlideController','addslide');
				$this->showtips('添加幻灯类别成功');
			}else{
				$this->showtips('操作失败');
			}
		}
		if(I('get.slide')){
			$slide = M('SlideEntity')
				   ->where(array(
						'slide_id' => I('get.slide')
					))->find();
				   
			$this->assign('slide',$slide);
		}
		
		$this->display('slide/addslide');
	}
	
	/*
	 * 添加幻灯片
	* @date: 2015-10-30下午2:00:19
	* @editor: XING
	*/
	public function addslidev(){
		if(I('post.dosubmit')){
			if(I('post.conid')){
				$ups = array(
						'title' => I('post.title'),
						'sort' => I('post.sort'),
						'update_at' => time(),
						'detail' => '',
						'pic_url' => I('post.pic_url'),
						'click_url' => I('post.click_url')
					);
				$affect = M('SlideContent')->where(array(
							'slide_content_id' => I('post.conid')
						  ))->setField($ups);
				if($affect){
					putlog('幻灯片更新成功 ID`'.I('post.conid').'`','SlideController','addslidev');
					$this->showtips('更新幻灯片成功');
				}else{
					$this->showtips('操作失败');
				}
			}
			$getAddData = array(
					'slide_id' => I('post.slide_id'),
					'pic_url' => I('post.pic_url'),
					'title' => I('post.title'),
					'sort' => I('post.sort'),
					'click_url' => I('post.click_url'),
					'add_at' => time(),
					'update_at' => time()
			);
	
			$addId = M('SlideContent')->add($getAddData);
			if($addId){
				putlog('幻灯片添加 ID`'.$addId.'`','SlideController','addslidev');
				$this->showtips('添加幻灯片成功');
			}else{
				$this->showtips('操作失败');
			}
		}
		if(I('get.conid')){
			$slide = M('SlideContent')
				->where(array(
					'slide_content_id' => I('get.conid')
				))->find();
				
			$this->assign('slide',$slide);
		}
	
		$this->display('slide/addslidev');
	}
	
	/*
	 * 幻灯删除
	 * @date: 2015-10-26下午3:38:47
	 * @editor: XING
	 */
	public function deleslide(){
		if(I('get.dtype')){
			switch(I('get.dtype')){
				/*幻灯类别*/
				case 'e':
					M('SlideEntity')->where(array(
						'slide_id' => I('get.did')
						))->delete();
					M('SlideContent')->where(array(
						'slide_id' => I('get.did')
						))->delete();
					break;
				/*幻灯内容*/
				case 'c':
					M('SlideContent')->where(array(
							'slide_content_id' => I('get.did')
						))->delete();
					break;
			}
			$this->showtips('删除成功');
		}else{
			$this->showtips('参数错误，操作失败');
		}
	}
}